<template>
  <!-- 放置表单 -->
  <el-form
    ref="userForm"
    :rules="rules"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top:30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input v-model="userInfo.password" style="width:300px" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="hSaveUserDetailById">更新</el-button>
      <el-button>返回</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees.js'

export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {},
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.loadUserInfoById()
  },
  methods: {
    async loadUserInfoById() {
      const data = await getUserDetailById(this.userId)
      this.userInfo = data
    },
    hSaveUserDetailById() {
      this.$refs.userForm.validate(async valid => {
        if (!valid) {
          return
        }
        // 使用最新修改数据覆盖源数据 作为接口提交数据
        await saveUserDetailById(this.userInfo)
        this.$message.success('更新成功')
      })
    }
  }
}
</script>
